﻿using InfluxData.Net.Common.Enums;
using InfluxData.Net.InfluxDb;
using InfluxData.Net.InfluxDb.Models.Responses;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SYSoft.Comm.Utils
{
    public class InfluxDbHelper
    {
        //声明InfluxDbClient
        private InfluxDbClient clientDb;
        string dbname="mtcd";

        public InfluxDbHelper()
        {
            //连接InfluxDb的API地址、账号、密码
            var infuxUrl = $"http://127.0.0.1:8086/";
            var infuxUser = "root";
            var infuxPwd = "root";

            //创建InfluxDbClient实例
            clientDb = new InfluxDbClient(infuxUrl, infuxUser, infuxPwd, InfluxDbVersion.Latest);
        }
        public InfluxDbHelper(string dbname)
        {
            this.dbname = dbname;
            //连接InfluxDb的API地址、账号、密码
            var infuxUrl = $"http://127.0.0.1:8086/";
            var infuxUser = "root";
            var infuxPwd = "root";

            //创建InfluxDbClient实例
            clientDb = new InfluxDbClient(infuxUrl, infuxUser, infuxPwd, InfluxDbVersion.Latest);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tablename">表名字</param>
        /// <param name="channelid">通道ID</param>
        /// <param name="hexdata"></param>
        public async void Adddata(string tablename, Dictionary<string, object> data)
        {
            var point_model = new InfluxData.Net.InfluxDb.Models.Point()
            {
                Name = tablename,
                Fields = data,
                Timestamp = DateTime.UtcNow.AddHours(8)
            };
            //从指定库中写入数据，支持传入多个对象的集合
            try
            {
                var response = await clientDb.Client.WriteAsync(point_model, dbname);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Influxdb写入失败：" + ex.Message);
            }

        }


        /// <summary>
        /// 从InfluxDB中读取数据 返回Serie，类似table ，有列集合 和数据集合
        /// </summary>
        public async Task<Serie> GetData(string queries)
        {
            //传入查询命令，支持多条
            //var queries = new[]
            //{
            //  " SELECT * FROM mytest WHERE time> now() - 24h "
            //};
            //var dbName = "mydbtest";
            IList<IList<object>> list = null;
            Serie serie=null;
            //从指定库中查询数据
            try
            {
      
                var response = await clientDb.Client.QueryAsync(queries, dbname);
                if (response.Count() > 0)
                {
                    var series = response.ToList();
                    //取出第一条命令的查询结果，是一个集合
                    serie = series[0];
                    //list = series[0].Values;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Influxdb读取失败：" + ex.Message);

            }

            //得到Serie集合对象（返回执行多个查询的结果）

            return serie;
        }



        /// <summary>
        /// 从InfluxDB中读取数据 返回Serie，类似table ，有列集合 和数据集合
        /// </summary>
        public async Task<int> GetDataCount(string queries)
        {
            //从指定库中查询数据
            try
            {

                var response = await clientDb.Client.QueryAsync(queries, dbname);
                return response.ToList()[0].Values.Count();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Influxdb读取失败：" + ex.Message);

            }

            //得到Serie集合对象（返回执行多个查询的结果）

            return 0;
        }
    }
}
